
//
//  WMEClubInformationTableViewCell.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/2/26.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEClubInformationTableViewCell.h"
#import <UIImageView+WebCache.h>
#import <Masonry.h>
#import <AFNetworking.h>

@interface WMEClubInformationTableViewCell ()
@property (nonatomic, retain)UIImageView *avatar;
@property (nonatomic, retain)UILabel *senderName;
@property (nonatomic, retain)UIView *commentsView;
@property (nonatomic, retain)UILabel *content;
@property (nonatomic, retain)UIImageView *reply_countImg;
@property (nonatomic, retain)UILabel *reply_count;
@property (nonatomic, retain)UIImageView *like_countImg;
@property (nonatomic, retain)UILabel *like_count;
@property (nonatomic, retain)UIButton *diandiandian;
@property (nonatomic, retain)UILabel *reply_name;
@property (nonatomic, retain)UILabel *gengduo;
@property (nonatomic, retain)UILabel *add_datetime_ts;
@property (nonatomic, retain)UILabel *line;
@end

@implementation WMEClubInformationTableViewCell

//@synthesize model = _model;
- (void)dealloc
{
    [_avatar release];
    [_senderName release];
    [_commentsView release];
    [_content release];
    [_reply_countImg release];
    [_reply_count release];
    [_like_countImg release];
    [_like_count release];
    [_reply_name release];
    [_gengduo release];
    [_add_datetime_ts release];
    [_line release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //发送的人的头像
        self.avatar = [[UIImageView alloc] init];
//        _avatar.backgroundColor = [UIColor yellowColor];
        _avatar.layer.cornerRadius = 20;
        _avatar.contentMode = UIViewContentModeScaleAspectFill;
        _avatar.clipsToBounds = YES;
        _avatar.userInteractionEnabled = YES;
        //轻拍手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarTapAction:)];
        [self.avatar addGestureRecognizer:tap];
        [self.contentView addSubview:_avatar];
        [_avatar release];
        [tap release];
        
        //发送的人名
        self.senderName = [[UILabel alloc] init];
//        _senderName.backgroundColor = [UIColor redColor];
        _senderName.textColor = [UIColor colorWithRed:0 green:160/255.0 blue:233/255.0 alpha:1];
        _senderName.font = [UIFont boldSystemFontOfSize:11];
        [self.contentView addSubview:_senderName];
        [_senderName release];
        
        //评论视图
        self.commentsView = [[UIView alloc] init];
        _commentsView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_commentsView];
        [_commentsView release];
        
        //主内容
        self.content = [[UILabel alloc] init];
//        _content.backgroundColor = [UIColor orangeColor];
        _content.numberOfLines = 0;
        _content.font = [UIFont systemFontOfSize:13];
        [self.commentsView addSubview:_content];
        [_content release];
        
        //点赞的图标
        self.like_countImg = [[UIImageView alloc] init];
//        _like_countImg.backgroundColor = [UIColor redColor];
        [self.commentsView addSubview:_like_countImg];
        [_like_countImg release];
        
        //点赞的人的个数
        self.like_count = [[UILabel alloc] init];
//        _like_count.backgroundColor = [UIColor lightGrayColor];
        _like_count.font = [UIFont systemFontOfSize:11];
        [self.commentsView addSubview:_like_count];
        [_like_count release];
        
        //评论个数的标签
        self.reply_countImg = [[UIImageView alloc] init];
//        _reply_countImg.backgroundColor = [UIColor lightGrayColor];
        [self.commentsView addSubview:_reply_countImg];
        [_reply_countImg release];
        
        //评论的个数
        self.reply_count = [[UILabel alloc] init];
//        _reply_count.backgroundColor = [UIColor blueColor];
        _reply_count.font = [UIFont systemFontOfSize:11];
        [self.commentsView addSubview:_reply_count];
        [_reply_count release];
        
        //3个点
        self.diandiandian = [UIButton buttonWithType:UIButtonTypeSystem];
//        _diandiandian.backgroundColor = [UIColor grayColor];
        [_diandiandian setImage:[UIImage imageNamed:@"222@2x"] forState:UIControlStateNormal];
        [self.commentsView addSubview:_diandiandian];
        [_diandiandian release];
        
        //评论的人名
        self.reply_name = [[UILabel alloc] init];
//        _reply_name.backgroundColor = [UIColor greenColor];
        _reply_name.font = [UIFont systemFontOfSize:11];
        _reply_name.numberOfLines = 0;
        [self.commentsView addSubview:_reply_name];
        [_reply_name release];
        
        //更多
        self.gengduo = [[UILabel alloc] init];
//        _gengduo.backgroundColor = [UIColor redColor];
        _gengduo.font = [UIFont systemFontOfSize:9];
        [self.commentsView addSubview:_gengduo];
        [_gengduo release];
        
        //美观用的线
        self.line = [[UILabel alloc] init];
        _line.backgroundColor = [UIColor lightGrayColor];
        [self.commentsView addSubview:_line];
        [_line release];
    }
    return self;
}
#pragma mark -重写setting方法
- (void)setModel:(WMEClubInformationModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
        [_avatar sd_setImageWithURL:[NSURL URLWithString:_model.senderAvatar]];
        
        _senderName.text = _model.senderName;
        
        
        _content.text = _model.content;
        
        _reply_countImg.image = [UIImage imageNamed:@"pinglun"];
        
        _reply_count.text = _model.reply_count.stringValue;
        
        _like_count.text = _model.like_count.stringValue;
        
        _like_countImg.image = [UIImage imageNamed:@"zan"];
        
        if (_model.reply_count.integerValue != 0) {
            
            _reply_name.text = [NSString stringWithFormat:@"%@ : %@", _model.repliesSenderName, _model.repliesContent];
            
            NSInteger i = _model.repliesSenderName.length;
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ : %@", _model.repliesSenderName, _model.repliesContent]];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0 green:160/255.0 blue:233/255.0 alpha:1] range:NSMakeRange(0, i)];
            _reply_name.attributedText = str;
            
            _gengduo.text = @"点击查看更多";
            [str release];
        }
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    __block __typeof(&*self)bs = self;
    _avatar.frame = CGRectMake(10, 0, 40, 40);
    //    [_avatar mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.size.mas_equalTo(CGSizeMake(40, 40));
    //        make.left.equalTo(bs.mas_left).with.offset(10);
    //        make.top.equalTo(bs.mas_top).with.offset(0);
    //    }];
    
    //    [_senderName mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(bs.mas_top).with.offset(0);
    //        make.left.equalTo(_avatar.mas_right).with.offset(10);
    ////        make.width.mas_equalTo();
    //        make.right.equalTo(bs.mas_right).with.offset(-100);
    //        make.height.equalTo(@20);
    //    }];
    _senderName.frame = CGRectMake(_avatar.frame.size.width + _avatar.frame.origin.x, 0, WIDTH - 100, 20);
    
    
    CGFloat contentHeight = [[self class] heightWithText:_model.content FontOfSize:13];
    _commentsView.frame = CGRectMake(_avatar.frame.size.width + 20, _senderName.frame.size.height, WIDTH - 70, contentHeight + 60);
    //    [_commentsView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(_senderName.mas_bottom).with.offset(0);
    //        make.left.equalTo(_avatar.mas_right).with.offset(10);
    //        make.right.equalTo(bs.mas_right).with.offset(-10);
    //        make.height.equalTo(@(contentHeight + 200));
    //    }];
    
    _content.frame = CGRectMake(10, 15, WIDTH - 90, contentHeight);
    //    [_content mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(_commentsView.mas_top).with.offset(10);
    //        make.left.equalTo(_commentsView.mas_left).with.offset(10);
    //        make.right.equalTo(_commentsView.mas_right).with.offset(10);
    //        make.height.equalTo(@(contentHeight + 8 * _content.numberOfLines));
    //    }];
    
    [_reply_countImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.top.equalTo(_content.mas_bottom).with.offset(20);
        make.left.equalTo(_commentsView.mas_left).with.offset(20);
    }];
    //    _reply_countImg.frame = CGRectMake(20, _content.frame.origin.y + _content.frame.size.height, 15, 15);
    
    [_reply_count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_content.mas_bottom).with.offset(20);
        make.left.equalTo(_reply_countImg.mas_right).with.offset(0);
        make.size.mas_equalTo(CGSizeMake((_commentsView.frame.size.width - 100) / 2, 15));
    }];
    //    _reply_count.frame = CGRectMake(_reply_countImg.frame.size.width + _reply_countImg.frame.origin.x, _content.frame.size.height + _content.frame.origin.y, (_commentsView.frame.size.width - 100) / 2, 15);
    
    [_like_countImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.top.equalTo(_content.mas_bottom).with.offset(20);
        make.left.equalTo(_reply_count.mas_right).with.offset(0);
    }];
    
    [_like_count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake((_commentsView.frame.size.width - 100) / 2, 15));
        make.top.equalTo(_content.mas_bottom).with.offset(20);
        make.left.equalTo(_like_countImg.mas_right).with.offset(0);
    }];
    
    [_diandiandian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.top.equalTo(_content.mas_bottom).with.offset(20);
        make.right.equalTo(bs.mas_right).with.offset(-20);
    }];
    
    if (_model.reply_count.integerValue != 0) {
        
        CGFloat contentHeight = [[self class] heightWithText:_model.content FontOfSize:13];
        _line.frame = CGRectMake(0, contentHeight + 20 + 15 + 10 + 20, WIDTH - 70, 0.4);
        NSLog(@"%@", _line);
        //        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.equalTo(_commentsView.mas_left);
        //            make.right.equalTo(_commentsView.mas_right);
        //            make.top.equalTo(_like_count.mas_bottom).with.offset(10);
        //            make.height.equalTo(@0.4);
        //        }];
        
        //        CGFloat replyNameWidth = [[self class] widthWithText:_model.repliesSenderName MySize:CGSizeMake(200, 20) MyFontSize:15];
        //        [_reply_name mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.equalTo(_commentsView.mas_left).with.offset(10);
        //            make.top.equalTo(_line.mas_bottom).with.offset(10);
        //            make.width.equalTo(@(replyNameWidth));
        //            make.height.equalTo(@20);
        //        }];
        
        CGFloat replyHeight = [[self class] heightWithText:[NSString stringWithFormat:@"%@%@", _model.repliesSenderName, _model.repliesContent] FontOfSize:11];
        _reply_name.frame = CGRectMake(10, _line.frame.size.height + _line.frame.origin.y + 5, WIDTH - 90, replyHeight);
        
        _gengduo.frame = CGRectMake(10, _reply_name.frame.size.height + _reply_name.frame.origin.y + 5, WIDTH - 20, 10);
        //        [_gengduo mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.equalTo(_commentsView.mas_left).with.offset(10);
        //            make.right.equalTo(_commentsView.mas_right).with.offset(-10);
        //            make.top.equalTo(_reply_name.mas_bottom).with.offset(10);
        //            make.height.equalTo(@10);
        //        }];
        
        _commentsView.frame = CGRectMake(_avatar.frame.size.width + 20, _senderName.frame.size.height, WIDTH - 70, contentHeight + replyHeight + 100);
        
    }else{
        _line.frame = CGRectMake(0, _like_count.frame.origin.y + _like_count.frame.size.height + 10, 0, 0);
        //        CGFloat replyHeight = [[self class] heightWithText:[NSString stringWithFormat:@"%@%@", _model.repliesSenderName, _model.repliesContent]];
        _reply_name.frame = CGRectMake(10, _line.frame.size.height + _line.frame.origin.y, 0, 0);
        
        _gengduo.frame = CGRectMake(10, _reply_name.frame.size.height + _reply_name.frame.origin.y, 0, 0);
    }
}
#pragma mark - 头像点击
- (void)avatarTapAction:(UITapGestureRecognizer *)tap
{
    NSString *userTemp = @"http://www.duitang.com/napi/people/profile/?__dtac=%257B%2522_r%2522%253A%2520%2522998121%2522%257D&_ts=1452865118.002704&app_code=gandalf&app_version=5.8%20rv%3A149591&device_name=iPhone%205&device_platform=iPhone5%2C2&include_fields=relationship%2Cbind_status%2Cinterests%2Cidentity%2Ccity%2Ccitycode%2Cgender%2Cbirthday%2Crecommend_info%2Cclub_count&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=568&screen_width=320&user_id=11200118";
    NSString *userStr = [userTemp stringByReplacingOccurrencesOfString:@"11200118" withString:[_model.senderId stringValue]];
    //    NSLog(@"%@", userStr);
    //AFN网络请求
    AFHTTPSessionManager *userData = [AFHTTPSessionManager manager];
    [userData GET:userStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //        NSLog(@"%@", responseObject);
        NSMutableDictionary *userDic = responseObject[@"data"];
        [self.delegate userAvatarPushAction:[_model.senderId stringValue] userDic:userDic];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}

//计算文本高
+ (CGFloat)heightWithText:(NSString *)text FontOfSize:(CGFloat )fontOfSize
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
    //最大范围
    CGSize size = CGSizeMake(WIDTH - 100, 1000);
    //计算 文本 需的rectangular
    CGRect r = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回文本 的 高
    return r.size.height;
}


//计算文本宽
+ (CGFloat)widthWithText:(NSString *)text MySize:(CGSize)size MyFontSize:(CGFloat)fontSize
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    //    CGSize size = CGSizeMake(WIDTH - 100, 10);
    CGRect r = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return r.size.width;
}


- (void)awakeFromNib {
    // Initialization code
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
